package com.jack.aggragate;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.jack.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import rx.Observable;

@Component
public class AggragationService {

	@Autowired
	private RestTemplate restTemplate;
	
	@HystrixCommand(fallbackMethod = "fallback")
	public Observable<User> getUserById(Long id){
		return Observable.create(observer ->{
			User user = restTemplate.getForObject("http://microservice-provider-user/" + id, User.class);
			observer.onNext(user);
			observer.onCompleted();
		});
	}
	
	@HystrixCommand(fallbackMethod = "fallback")
	public Observable<User> getMovieById(Long id){
		return Observable.create(observer ->{
			User movieUser = restTemplate.getForObject("http://microservice-consumer-movie/user/" + id, User.class);
			observer.onNext(movieUser);
			observer.onCompleted();
		});
	}
	
	public User fallback(Long id){
		User user = new User();
		user.setId(-1L);
		user.setName("default name");
		return user;
	}
}
